package com.beyondbit.hr.utils.mybatis;

import com.beyondbit.hr.api.dto.Pageable;
import com.beyondbit.hr.api.utils.BeanUtils;
import com.beyondbit.hr.api.utils.RollbackException;


import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class SQLUtils {


    Object reqObject;
    String objName;

    public SQLUtils(Object reqObject, String objName) {
        this.reqObject = reqObject;
        this.objName = objName;
    }

    public SQLUtils(Map<String, Object> map,String objName){
        this.objName = objName;
        this.reqObject = map.get(objName);
    }

    public Object getReqObject() {
        return reqObject;
    }

    public void setReqObject(Object reqObject) {
        this.reqObject = reqObject;
    }

    public String equals(String columnName, String propertyName) {
        return String.format("%s = %s", columnName, normalRightValue(objName, propertyName));
    }

    public String notEquals(String columnName, String propertyName) {
        return String.format("%s <> %s", columnName, normalRightValue(objName, propertyName));
    }

    public String isNull(String columnName) {
        return String.format("%s is null", columnName);
    }

    public String notNull(String columnName) {
        return String.format("%s is not null", columnName);
    }

    public String in(String columnName, String propertyName) {
        return String.format("%s in (%s)", columnName, inRightValue(objName, propertyName, reqObject));
    }

    public String greaterThan(String columnName, String propertyName) {
        return String.format("%s > %s", columnName, normalRightValue(objName, propertyName));
    }

    public String greaterThanOrEqual(String columnName, String propertyName) {
        return String.format("%s >= %s", columnName, normalRightValue(objName, propertyName));
    }

    public String lessThan(String columnName, String propertyName) {
        return String.format("%s < %s", columnName, normalRightValue(objName, propertyName));
    }

    public String lessThanOrEqual(String columnName, String propertyName) {
        return String.format("%s <= %s", columnName, normalRightValue(objName, propertyName));
    }

    public String limitAndOffset(String sql){
        String returnValue = sql;
        if (getReqObject() instanceof Pageable){
            Pageable pageable = (Pageable) getReqObject();
            Integer pageSize = pageable.getPageSize();
            Integer page = pageable.getPage();
            if ( pageSize != null ){
                returnValue = String.format( "%s limit %d",sql,pageSize);
                if (page != null){
                    returnValue = String.format( "%s offset %d",returnValue,page*pageSize);
                }
            }
        }

        return returnValue;
    }


    public static String inRightValue(String objName, String propertyName, Object obj) {
        try {
            Object value = BeanUtils.propertyValue(obj, propertyName);
            if (value instanceof Collection) {
                Collection<?> c = (Collection<?>) value;
                int size = c.size();
                List<String> items = new LinkedList<>();
                for (int i = 0; i < size; i++) {
                    items.add(String.format("#{%s.%s[%d]}", objName, propertyName, i));
                }
                return String.join(",", items);
            } else {
                throw new RollbackException(propertyName + "属性不是集合对象");
            }
        } catch (Throwable e) {
            throw new RollbackException(e);
        }
    }

    public static String normalRightValue(String objName, String propertyName) {
        return String.format("#{%s.%s}", objName, propertyName);
    }


}
